“h5_hype”

Information

The example shown in this file appears at: http://www.hdfgroup.org/HDF5/Tutor/selectsimple.html (accessed 10 July 2011)

The example shown in this file is following the tutorial: http://www.hdfgroup.org/HDF5/Tutor/introductory.html

Good references for the shown HDF5 functions include:
http://www.hdfgroup.org/HDF5/doc/RM/RM_H5Front.html
http://www.hdfgroup.org/HDF5/doc/RM/

Prepared 10 July 2011 by Scot Martin for Windows 7, 64-bit, Mathematica version 8.0.1.

“c” code we seek to replicate in this example

/*************************************************************
 *  
 * This example shows how to write a subset of data (a slab) to
 * a dataset in an HDF5 file.
 *
 *************************************************************/
 
#include "hdf5.h"

#define FILE        "hype.h5"
#define DATASETNAME "IntArray" 
#define RANK  2

/* Note: changing these values modifies the subset size. */

#define DIM0_SUB  3                 /* subset dimensions */ 
#define DIM1_SUB  4 

/* End Note */

#define DIM0     8                 /* size of dataset */       
#define DIM1     10 

int
main (void)
{
    hsize_t     dims[2], dimsm[2];   
    int         data[DIM0][DIM1];           /* data to write */
    int         sdata[DIM0_SUB][DIM1_SUB];  /* subset to write */
    int         rdata[DIM0][DIM1];          /* buffer for read */
 
    hid_t       file_id, dataset_id;        /* handles */
    hid_t       dataspace_id, memspace_id; 

    herr_t      status;                             
   
    hsize_t     count[2];              /* size of subset in the file */
    hsize_t     offset[2];             /* subset offset in the file */
    hsize_t     stride[2];
    int         i, j;


    /*****************************************************************
     * Create a new file with default creation and access properties.
     * Then create a dataset and write data to it and close the file
     * and dataset. 
     *****************************************************************/

    file_id = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    dims[0] = DIM0;
    dims[1] = DIM1;
    dataspace_id = H5Screate_simple (RANK, dims, NULL); 

    dataset_id = H5Dcreate (file_id, DATASETNAME, H5T_STD_I32BE, dataspace_id,
                         H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);


    for (j = 0; j < DIM0; j++) {
	for (i = 0; i < DIM1; i++)
            if (i< (DIM1/2))
	       data[j][i] = 1;
            else
               data[j][i] = 2;
    }     

    status = H5Dwrite (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                      H5P_DEFAULT, data);

    printf ("\nData Written to File:\n");
    for (i = 0; i<DIM0; i++){
       for (j = 0; j<DIM1; j++)
           printf (" %i", data[i][j]);
       printf ("\n");
    }
    status = H5Sclose (dataspace_id);
    status = H5Dclose (dataset_id);
    status = H5Fclose (file_id);


    /*************************************************************
     * Reopen the file and dataset and write a subset of data to
     * the dataset.
     *************************************************************/

    file_id = H5Fopen (FILE, H5F_ACC_RDWR, H5P_DEFAULT);
    dataset_id = H5Dopen (file_id, DATASETNAME, H5P_DEFAULT);

    /*************************************************************
     * Specify size and shape of subset to write.
     *************************************************************/

    offset[0] = 1;
    offset[1] = 2;

    count[0]  = DIM0_SUB;  
    count[1]  = DIM1_SUB;

    stride[0] = 1;
    stride[1] = 1;

    /*************************************************************
     * Create memory space with size of subset. Get file dataspace 
     * and select subset from file dataspace.             
     *************************************************************/

    dimsm[0] = DIM0_SUB;
    dimsm[1] = DIM1_SUB;
    memspace_id = H5Screate_simple (RANK, dimsm, NULL); 

    dataspace_id = H5Dget_space (dataset_id);
    status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
                                  stride, count, NULL);

    /*************************************************************
     * Write a subset of data to the dataset, then read the 
     * entire dataset back from the file. Close everything. 
     *************************************************************/

    for (j = 0; j < DIM0_SUB; j++) {
	for (i = 0; i < DIM1_SUB; i++)
	   sdata[j][i] = 5;
    }     

    status = H5Dwrite (dataset_id, H5T_NATIVE_INT, memspace_id,
                       dataspace_id, H5P_DEFAULT, sdata);
    
    status = H5Dread (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                       H5P_DEFAULT, rdata);

    printf ("\nData after Selection is Written:\n");
    for (i = 0; i<DIM0; i++){
       for (j = 0; j<DIM1; j++)
           printf (" %i", rdata[i][j]);
       printf ("\n");
    }


    status = H5Sclose (dataspace_id);
    status = H5Sclose (memspace_id);
    status = H5Dclose (dataset_id);
    status = H5Fclose (file_id);
 
}

Implementation in Mathematica

set up

SetDirectory[NotebookDirectory[]]

Needs["HDF5`"]

dims={8,10}

dimsSubset={3,4}

{offset,count,stride}={{1,2},dimsSubset,{1,1}}

data=
ConstantArray[
ConstantArray[1,dims[[2]]/2]~Join~ConstantArray[2,dims[[2]]/2],
dims[[1]]
]

subsetData=ConstantArray[5,dimsSubset]

file=FileNameJoin[{Directory[],"hype.h5"}]

Write Full Data Set

fileID=H5Fcreate[file,H5FuACCuTRUNC,H5PuDEFAULT,H5PuDEFAULT]

dataSpaceID=H5ScreateUsimple[Length@dims,dims,Null]

dataSetID=H5Dcreate[fileID,"IntArray",H5T`copy[H5TuSTDuI32BE]@Id[],dataSpaceID,H5PuDEFAULT,H5PuDEFAULT,H5PuDEFAULT]

H5Dwrite[Integer32][dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,H5SuALL,H5PuDEFAULT,Flatten@data]

H5Sclose[dataSpaceID]
H5Dclose[dataSetID]
H5Fclose[fileID]

Write to Subregion of Data Set

fileID=H5Fopen[file,H5FuACCuRDWR,H5PuDEFAULT]

dataSetID=H5Dopen[fileID,"IntArray",H5PuDEFAULT]

memorySpaceID=H5ScreateUsimple[Length@dimsSubset,dimsSubset,Null]

dataSpaceID=H5DgetSpace[dataSetID]

H5SselectHyperSlab [dataSpaceID, H5SuSET, offset,stride, count, Null]

H5Dwrite[Integer32][dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],memorySpaceID,dataSpaceID,H5PuDEFAULT,Flatten@subsetData]

readDataObject=MakeNETObject[Flatten@data]

H5Dread[Integer32][dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,H5SuALL,H5PuDEFAULT,readDataObject]

NETObjectToExpression@readDataObject

Partition[%,Rest@dims]

Close items

H5Sclose[dataSpaceID]
H5Sclose[memorySpaceID]
H5Dclose[dataSetID]
H5Fclose[fileID]